<?php
namespace wof;

class User {
    private $db;
    public $a_PlayerData = array();
    
    public function __construct($a_PlayerData) {
        $this->db = new DBConfig;
        $this->a_PlayerData = $a_PlayerData;
    }
    
    /* Validate user log-on and check limitation of player then send to clients */
    public function validateUser ($i_ConnId, $sz_Info) {
        $a_Ret = array();
        if ($sz_Info) {
            $a_Elements = explode("%%S%%", $sz_Info);
            $i_UserId = $a_Elements[0];
            $sz_SessionId = $a_Elements[1];
            $sz_Ticket = $a_Elements[2]; 
            
            // get user
            $o_User = $this->getUserById($i_UserId);
            // generate ticket
            $sz_GenerateTicket = base64_encode($o_User->user_username . $o_User->user_password . $sz_SessionId . 'ABCXYZ');
            // Validate user log-on
            if ($sz_Ticket != $sz_GenerateTicket) {
                $a_Ret['control'] = "error";
                $a_Ret['value'] = "Please log in for play game!";
            }
            else {
                /* If game is not playing and is not reach limitation, add to player list */
                if ($this->a_PlayerData['status'] != "play") {
                    if (!$this->a_PlayerData['a_Player'] || count($this->a_PlayerData['a_Player']) < 3 ) {
                        $a_player = array();
                        $a_player['id'] = $i_UserId;
                        $a_player['username'] = $o_User->user_username .';' .$o_User->user_avatar;
                        $a_player['connId'] = $i_ConnId;
                        $this->a_PlayerData['a_Player'][] = $a_player;
                        
                        $a_Return = array();
                        for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                            $a_Return[] = $this->a_PlayerData['a_Player'][$i]['username'];
                        }
                        $a_Ret['control'] = "user";
                        $a_Ret['value'] = implode(",",$a_Return);
                    }
                    else {
                        $a_Ret['control'] = "error";
                        $a_Ret['value'] = "Players is limited!";
                    }
                }
                else {
                    $a_Ret['control'] = "error";
                    $a_Ret['value'] = "Could not join game when the game is started!";
                }
            }
        }
        return $a_Ret;
    }
    
    /* Get user score list then send to clients */
    public function getScoreList () {
        $sz_Return = "";
        $a_Return = array();
        if ($this->a_PlayerData['a_Player']) {
            for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                $a_Return[] = $this->a_PlayerData['a_Player'][$i]['username'] . ',' . $this->a_PlayerData['a_Player'][$i]['score'];
            }
            $sz_Return = implode("%%S%%",$a_Return);
        }
        return $sz_Return;
    }
    
    /* Remove user from player list then send list to clients */
    public function removeUser ($i_ConnId) {
        $sz_Return = "";
        $a_Return = array();
        if ($this->a_PlayerData['a_Player']) {
            for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                if ($this->a_PlayerData['a_Player'][$i]['connId'] != $i_ConnId) {
                    $a_Return[] = $this->a_PlayerData['a_Player'][$i]['username'];
                }
                else {
                    array_splice($this->a_PlayerData['a_Player'], $i, 1);
                }
            }
            $sz_Return = implode(",",$a_Return);
        }
        return $sz_Return;
    }
    
    /* Get user from DB */
    public function getUserById($i_ID) {
        $o_User = new \stdClass();
        $sql="SELECT * FROM wof_user WHERE user_id=?";
        $statement = $this->db->pdo->prepare($sql);
        $statement->execute(array($i_ID));
        
        $o_User = $statement->fetchObject();
        
        return $o_User;
    }
    
    /* Get final user score list of round then send to clients */
	public function endRound () {
        $sz_Return = "";
        if ($this->a_PlayerData['a_Player']) {
            $i_Score = 0;
            for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                if ($this->a_PlayerData['a_Player'][$i]['username'] == $this->a_PlayerData['play_turn_name']) {
                    $i_Score = $this->a_PlayerData['a_Player'][$i]['score'];
                }
            }
        }
        
        if ($i_Score == 0) {
         $i_Score = 200;
        }
        $sz_Return = $this->a_PlayerData['play_turn_name'] . "," . $i_Score;
        return $sz_Return;
    }
}

?>